﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ECRMS.WinForm.BaseClass;
using ECRMS.WinForm.BaseClass.Common;
using ECRMS.WinForm.Properties;
using ECRMS.WinForm.WinService;
using ECRMS.WinForm.ModuleEdits;
using System.Linq;

namespace ECRMS.WinForm.Modules
{
   [ModuleInstance((int)ModuleEnum.账户管理)]
    public partial class OrgEmployeeModudle : BaseModule
    {
       public OrgEmployeeModudle()
        {

            InitializeComponent();
            base.core.CreateWaitDialog("初始化中请稍候", "员工管理");
            InitView();
            InitData();
            core.CloseWaitDialog();
        }

       //true 为不需要去服务端重新获取数据
       private void InitData(bool isPostBack=false)
       {
           if (!isPostBack)
           {
               FuncResult result = core.CoreEmployee.GetEmployees(core.CoreUser.UserInfo.CorporationRefId);
               if (result.Status == 0)
               {
                   XtraMessageBox.Show(this, Language_ch.ResourceManager.GetString(result.Message));
                   return;
               }
           }
           this.gdemployee.DataSource = core.CoreEmployee.Employees;
           this.gdemployee.RefreshDataSource();
       }

        private void InitView()
        {
            base.selectBarButtonVisiable = false;
            base.saveBarButtonVisiable = false;
            base.otherBarButton1Visiable = false;
            base.otherBarButton2Visiable = false;
            base.otherBarButton3Visiable = false;
            base.outBarButtonVisiable = false;
        }
       //新增
        public override void newBarButton_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.newBarButton_ItemClick(sender, e);
            core.CoreEmployee.OperatorRow = new CtEmployee();
            core.CoreEmployee.OperatorRow.UserInfo = new CtUser();
            core.CoreEmployee.OperatorRow.UserInfo.UserRefId = Guid.NewGuid();

            core.CoreEmployee.OperatorRowCtFunctions = new List<int>();

            EmployeeEdit employeeEdit = new EmployeeEdit(this.RectangleToScreen(Bounds), OperationEnum.New);
            DialogResult result = employeeEdit.ShowDialog();

            if (result == DialogResult.OK)
            {
                InitData(true);
            }

            core.CoreEmployee.OperatorRow = null;
            core.CoreEmployee.OperatorRowCtFunctions = null;
            employeeEdit.Dispose();

        }

       //修改
        public override void editBarButton_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            base.editBarButton_ItemClick(sender, e);
            CtEmployee employee = core.CoreEmployee.Employees.FirstOrDefault(p => p.UserInfo.UserRefId == core.CoreCommon.GetGridFocusRowRefId(gdemployee, gridView1, "UserInfo.UserRefId"));
         
                core.CoreEmployee.OperatorRow = employee;
           
            core.CoreEmployee.OperatorRowCtFunctions = employee.Functions.ToList();
            core.CoreEmployee.OperatorRow.Functions = employee.Functions;
            EmployeeEdit employeeEdit = new EmployeeEdit(this.RectangleToScreen(Bounds), OperationEnum.Modify);
            DialogResult result = employeeEdit.ShowDialog();

            if (result == DialogResult.OK)
            {
                InitData(true);
            }

            core.CoreEmployee.OperatorRow = null;
            core.CoreEmployee.OperatorRowCtFunctions = null;
            employeeEdit.Dispose();

        }
    }
}
